package com.batchbrake.queue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.JXTable;

import com.batchbrake.jobs.JobProc;
import com.batchbrake.main.BatchBrakeProc;

public class QueuePanel extends JXPanel implements Observer {
	private QueueTableModel model = null;
	private QueueTable table = null;
	private JScrollPane scroll = null;
	
	public QueuePanel(BatchBrakeProc proc) {
		proc.addObserver(this);
		this.initComponents(proc);
	}


	private void initComponents(BatchBrakeProc proc) {
		table = new QueueTable();
		table.setBackground(Color.white);
		table.setGridColor(Color.white);
		table.setShowGrid(true, false);
		table.setTableHeader(null);
		table.setRowHeightEnabled(true);
		table.setCellSelectionEnabled(true);
		table.setRowHeight(70);
		table.setBorder(BorderFactory.createEmptyBorder());
		
		model = new QueueTableModel(proc);
		table.setModel(model);
		
		scroll = new JScrollPane(table);
		scroll.setBackground(Color.white);
		scroll.setViewportBorder(BorderFactory.createEmptyBorder(1,2,0,0));
		scroll.setBorder(BorderFactory.createEmptyBorder());
		this.setLayout(new BorderLayout());
		this.add(scroll, BorderLayout.CENTER);
	}
	
	public void setTimerEnabled(boolean enabled) {
		table.setTimerEnabled(enabled);
	}
	
	public QueueEntity[] getSelectedRows() {
		int[] rows = table.getSelectedRows();
		QueueEntity[] entities = new QueueEntity[rows.length];
		
		for( int i = 0; i < rows.length; i++ ) {
			entities[i] = (QueueEntity) table.getValueAt(rows[i], 0);
		}
		return entities;
	}


	@Override
	public void update(Observable o, Object arg) {
		if( (o instanceof BatchBrakeProc) && !(arg instanceof JobProc) ) { 
			BatchBrakeProc p = (BatchBrakeProc) o;
			table.setRowHeightEnabled(false);
			table.repaint();
			table.setRowHeightEnabled(true);
		}
	}

	
	
}
